package leetcode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author Liangyaoyun
 */
public class leetCode111 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public int minDepth(TreeNode root) {
        Queue<TreeNode>queue=new LinkedList<>();
        int count=0;
        LinkedList<LinkedList<Integer>>res=new LinkedList<>();
        if (root!=null){
            queue.offer(root);
        }
        while (!queue.isEmpty()){
            int n=queue.size();
            for (int i = 0; i < n; i++) {
                TreeNode node=queue.poll();
                if (node.left!=null){
                    queue.offer(node.left);
                }
                if (node.right!=null){
                    queue.offer(node.right);
                }
                if (node.left==null&&node.right==null){
                    return count+1;
                }
            }
            count++;
        }
        return count;
    }
}
